#include "runtaskthread.h"

RunTaskThread::RunTaskThread(QVector<Point> *p,int count,double tInit,double tFinal,double a)
{
    this->pointArr=p;
    this->tInit=tInit;
    this->tFinal=tFinal;
    this->alpha=a;
    this->count=count;
}

void RunTaskThread::run()
{
    QString tourBestStr;
    QVector<int> res;
    double valueBestNow=DBL_MAX;
    auto start=clock();
    //        Utils task(*this->pointArr,tInit,tFinal,alpha);
    //        task.runTSP();
    //        if(task.getBestValue()<valueBestNow){
    //            valueBestNow=task.getBestValue();
    //            tourBestStr=task.getTourBestStr();
    //            res=task.getTourBest();
    //        }
    locker=new QMutex();
    for(int i=0;i<count;i+=16){
        QVector<worker*> workers;
        auto num=count-i<16?count-i:16;
        for(int j=0;j<num;j++){
            workers.push_back(new worker(this->pointArr,&valueBestNow,&tourBestStr,&res,tInit,tFinal,alpha,locker));
            workers.back()->start();
        }
        for(auto w:workers){
            w->wait();
            w->deleteLater();
        }
    }
    delete locker;
    auto end=clock();
    double dur=static_cast<double>(end-start)/CLOCKS_PER_SEC;
    auto resLog="最好路径为:"+tourBestStr+"\n长度:"+QString::number(valueBestNow)+"\n计算用时"+QString::number(dur)+"s";
    qRegisterMetaType<QVector<int>>("QVector<int>");
    emit tTaskFinished(res,resLog);
}
